<template>
  <div>
    <van-search shape="round"
                show-action
                v-model="search_text">
      <template #left>
        <div>
          <van-icon name="arrow-left"
                    size="35"></van-icon>
        </div>
      </template>
      <template #action>
        <div>
          <van-button type="primary"
                      @click="search">搜索</van-button>
        </div>
      </template>
    </van-search>
    <div class="zuijin">
      <div>
        <span>最近搜索</span> <span @click="clear"
              style="font-size:30px;">×</span>
      </div>
      <div style="margin-top:10px;">
        <van-tag type="primary"
                 v-for="(item,index) in searchArr"
                 :key="index"
                 @click="turnList(item)">{{item}}</van-tag>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  created () {
    if (localStorage.searchArr) {
      this.searchArr = JSON.parse(localStorage.searchArr);
    } else {
      localStorage.searchArr = JSON.stringify([]);
    }
  },
  methods: {
    search () {  //搜索
      this.searchArr.push(this.search_text);
      localStorage.searchArr = JSON.stringify(this.searchArr)
      // 跳转搜索列表页 并传参
      this.$router.push({ name: "searchList", params: { keywords: this.search_text } })
    },
    clear () { //清空
      localStorage.searchArr = JSON.stringify([]);
      this.searchArr = [];
    },
    turnList (item) {
      // 跳转搜索列表页 并传参
      this.$router.push({ name: "searchList", params: { keywords: item } })
    }
  },
  data () {
    return {
      search_text: "",
      searchArr: []//最近搜索
    }
  }
}
</script>
<style lang="less">
.zuijin {
  div:nth-child(1) {
    padding: 0 30px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
  }
  .van-tag {
    margin-left: 20px;
  }
}
</style>